package com.david.rabbitmq.direct;

import com.david.rabbitqm.util.ChannelUtlis;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;

import java.io.IOException;

/**
 * @author David
 * @version V1.0
 * @Package com.david.rabbitmq.direct
 * @date 2021/7/17 10:09
 */
public class DirectProducer {
    public static void main(String[] args) throws IOException {
        Channel channel = ChannelUtlis.getChannel();
        channel.exchangeDeclare("direct_logs", BuiltinExchangeType.DIRECT);
        String servrity = null;
        for (int i = 0; i < 100; i++) {
            switch (i % 3) {
                case 0:
                    servrity = "info";
                    break;
                case 1:
                    servrity = "warn";
                    break;
                case 2:
                    servrity = "error";
                    break;
                default:
                    System.err.println("log错误，程序退出");
                    System.exit(-1);
            }
            String logStr = "这是 【" + servrity + "】 的消息";
            //第二个参数为routingKey
            channel.basicPublish("direct_logs", servrity, null, logStr.getBytes("UTF-8"));
            System.out.println("发送消息：" + logStr);
        }
    }
}
